#include "bitset.h"
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cassert>

/*
void test1(){
    int x1{666};
    cout << x1 << endl;
}
*/

/*
void test2(){
    vector<int> v1 = {1,2,3,4,5,6};
    vector<int> v2{10,20,30,40,50,60};

    list<int> lt1 = {1,2,3,4,5,6};
    list<int> lt2{10,20,30,40,50,60};

    //常量数组在常量区存下来，两个指针指向其起始和结尾
    auto it1 = {1,2,3,4,5,6};
    cout << typeid(it1).name() << endl;

    initializer_list<int>::iterator it = it1.begin();
    cout << it << endl;
    //指向的内容在常量区，不可修改
    //(*it)++;
}
*/

namespace LuCopy
{
    class string
    {
    public:
        typedef char* iterator;
        iterator begin()
        {
            return _str;
        }

        iterator end()
        {
            return _str + _size;
        }

        string(const char* str = "")
                :_size(strlen(str))
                , _capacity(_size)
        {
            //cout << "string(char* str)" << endl;

            _str = new char[_capacity + 1];
            strcpy(_str, str);
        }

        // s1.swap(s2)
        void swap(string& s)
        {
            ::swap(_str, s._str);
            ::swap(_size, s._size);
            ::swap(_capacity, s._capacity);
        }

        // 拷贝构造
        string(const string& s)
                :_str(nullptr)
        {
            cout << "string(const string& s) -- deepCopy" << endl;

            string tmp(s._str);
            swap(tmp);
        }

        // 移动构造
        string(string&& s)
                :_str(nullptr)
        {
            cout << "string(string&& s) -- moveCopy" << endl;
            swap(s);
        }

        // 赋值重载
        string& operator=(const string& s)
        {
            cout << "string& operator=(string s) -- deepCopy" << endl;
            string tmp(s);
            swap(tmp);

            return *this;
        }

        ~string()
        {
            delete[] _str;
            _str = nullptr;
        }

        char& operator[](size_t pos)
        {
            assert(pos < _size);
            return _str[pos];
        }

        void reserve(size_t n)
        {
            if (n > _capacity)
            {
                char* tmp = new char[n + 1];
                strcpy(tmp, _str);
                delete[] _str;
                _str = tmp;

                _capacity = n;
            }
        }

        void push_back(char ch)
        {
            if (_size >= _capacity)
            {
                size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
                reserve(newcapacity);
            }

            _str[_size] = ch;
            ++_size;
            _str[_size] = '\0';
        }

        //string operator+=(char ch)
        string& operator+=(char ch)
        {
            push_back(ch);
            return *this;
        }

        string operator+(char ch)
        {
            string tmp(*this);
            tmp += ch;
            return tmp;
        }

        const char* c_str() const
        {
            return _str;
        }
    private:
        char* _str;
        size_t _size;
        size_t _capacity; // 不包含最后做标识的\0
    };

    LuCopy::string to_string(int value)
    {
        bool flag = true;
        if (value < 0)
        {
            flag = false;
            value = 0 - value;
        }

        LuCopy::string str;
        while (value > 0)
        {
            int x = value % 10;
            value /= 10;

            str += ('0' + x);
        }

        if (flag == false)
        {
            str += '-';
        }

        std::reverse(str.begin(), str.end());
        return str;
    }
}

void test1(){
    LuCopy::string s1("hello world");

    LuCopy::string ret1 = s1;

    //LuCopy::string ret2;
    //ret2 = ret1;

    LuCopy::string ret2 = (s1 + '!');
}

int main() {
    //test_bitset1();
    //test_twoBitset();
    //test_BloomFilter1();
    test1();
    return 0;
}
